const app = getApp()
const Api = require("../../../utils/api.js")
const ApiRequest = require("../../../utils/request.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    basicsList: [{
      icon: 'roundcheckfill',
      color: 'green',
      name: '已完成'
    }, {
      icon: 'usefullfill',
      color: 'blue',
      name: '进行中'
    }, {
      icon: 'radioboxfill',
      color: 'gray',
      name: '未完成'
    }, ],
    basics: 0,
    modalName: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this
    let flowID = options.id
    //测试
    // let flowID = '7'
    this.setData({
      flowID: flowID,
      height: wx.getSystemInfoSync().windowHeight
    })
  },
  getFlow: function () {
    var self = this
    var url = Api.getFlow + '/' + self.data.flowID
    var params = {
      flowId: self.data.flowID
    };
    var method = 'GET';
    var header = "application/json";
    var success = function (res) {
      let data = res.data
      console.log("getFlow res", res)
      let currentID = self.getCurrentItemID(data.flowStepList)
      self.setData({
        flowInfo: data,
        flowStepList: data.flowStepList,
        currentID: currentID
      })
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },
  // 获取“进行中”的id
  getCurrentItemID(flowStepList) {
    let res = null
    for (var i = 0; i < flowStepList.length; i++) {
      if (flowStepList[i].state == 1) {
        res = 'currentID_' + flowStepList[i].stepId + '_1'
        break;
      }
    }
    if (res == null) {
      res = 'currentID_' + flowStepList[flowStepList.length - 1].stepId + '_2'
    }
    return res
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 确保返回该页面的时候刷新数据
    this.getFlow()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  fail: function (e) {
    console.log("请求失败：", e)
  },
  scrollSteps() {
    this.setData({
      scroll: this.data.scroll == 5 ? 0 : this.data.scroll + 1
    })
  },
  /**
   * 操作选择(查看病人信息|查看医生信息|查看问卷|填写问卷)
   */
  selcetOptions: function (e) {
    var self = this
    let type = e.currentTarget.dataset.type
    let state = e.currentTarget.dataset.state
    let flowStepId = e.currentTarget.dataset.flowstepid
    console.log("type: ", type)

    if (type > 0) { //填写问卷
      self.toSurvey(type, state, flowStepId)
    } else {
      if (type == -1) { //查看自己信息
        self.getPersonInfo()
      } else { //查看医师信息
        self.getDoctorInfo()
      }
    }
  },
  hideInfo: function (e) {
    this.setData({
      modalName: null
    })
  },
  getPersonInfo: function () {
    console.log("完善病人信息")
    let self = this
    self.setData({
      modalName: 'personInfo'
    })
  },
  getDoctorInfo: function () {
    var self = this
    wx.navigateTo({
      url: '../doctorInfo/doctorInfo?flowID=' + self.data.flowInfo.id,
    })
  },
  toSurvey: function (type, state, flowStepId) {
    // console.log("type: ", type)
    // console.log("state: ", state)
    wx.navigateTo({
      url: '../../survey/survey?state=' + state + '&type=' + type + '&flowStepId=' + flowStepId + '&role=patient',
    })
  },
  /**
   * 跳转到聊天反馈界面
   */
  toChat: function () {
    var flowID = this.data.flowID
    wx.navigateTo({
      url: '../../chat/chat?flowID=' + flowID,
    })
  },
  /**
   * 刷新flow信息
   */
  refreshFlow: function () {
    this.getFlow()
    wx.showToast({
      title: '刷新成功',
    })
  }
})